package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.LinkedList;
import java.util.List;

/**
 *  cskaoyan
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {


    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter converter;


//    /**
//     * wukunli
//     */
//    @Override
//    public OrderListResponse queryOrder(OrderListRequest orderListRequest) {
//        OrderListResponse response = new OrderListResponse();
//        try {
//            orderListRequest.requestCheck();
//            Example example = new Example(Order.class);
//            example.createCriteria().andEqualTo("userId", orderListRequest.getUserId());
//            List<Order> orders = orderMapper.selectOrderListByUserId(orderListRequest.getUserId());
//
//            response.setDetailInfoList(new LinkedList<>());
//            for (Order order : orders) {
//                OrderDetailInfo orderDetailInfo = converter.order2detail(order);
//                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
//
//                List<OrderItemDto> itemDtos = converter.item2dto(orderItems);
//                orderDetailInfo.setOrderItemDto(itemDtos);
//
//                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
//                orderDetailInfo.setOrderShippingDto(converter.shipping2dto(orderShipping));
//
//                response.setCode(SysRetCodeConstants.SUCCESS.getCode());
//                response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
//                response.getDetailInfoList().add(orderDetailInfo);
//            }
//            response.setTotal((long) response.getDetailInfoList().size());
//        }catch (Exception e){
//            log.error("com.mall.order.services.OrderQueryServiceImpl occur Exception :"+ e);
//            ExceptionProcessorUtils.wrapperHandlerException(response,e);
//        }
//        return response;
//    }

    /**
     * lxy
     * 显示用户所有订单
     * @param orderListRequest
     * @return
     */
    @Override
    public OrderListResponse queryOrder(OrderListRequest orderListRequest) {
        OrderListResponse response = new OrderListResponse();
        try {
            orderListRequest.requestCheck();
            List<OrderDetailInfo> orderDetailInfos = new LinkedList<>();
            // 配置分页信息
            PageHelper.startPage(orderListRequest.getPage(), orderListRequest.getSize());
            // 构造条件数据
            Example example=new Example(Order.class);
            example.createCriteria().andEqualTo("userId",orderListRequest.getUserId());
            // 设置排序
            String sort = orderListRequest.getSort();
            if(sort != null && "".equals(sort)){
                example.setOrderByClause(sort);
            }
            List<Order> orders = orderMapper.selectByExample(example);

            PageInfo<Order> pageInfo = new PageInfo<>(orders);

            for (Order order : orders) {
                OrderDetailInfo orderDetailInfo = converter.order2detail(order);
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
                List<OrderItemDto> itemDtos = converter.item2dto(orderItems);
                orderDetailInfo.setOrderItemDto(itemDtos);
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
                orderDetailInfo.setOrderShippingDto(converter.shipping2dto(orderShipping));
                response.setCode(SysRetCodeConstants.SUCCESS.getCode());
                response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
                orderDetailInfos.add(orderDetailInfo);
                response.setData(orderDetailInfos);
            }
            // 订单总数
            response.setTotal(pageInfo.getTotal());
        }catch (Exception e){
            log.error("com.mall.order.services.OrderQueryServiceImpl occur Exception :"+ e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    /**
     * 显示订单详情
     * @param request
     * @return
     */
    @Override
    public OrderIdDetailResponse queryOrderDetail(OrderDetailRequest request) {
        OrderIdDetailResponse response = new OrderIdDetailResponse();
        try {
            request.requestCheck();
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());
            response.setUserId(order.getUserId());

            response.setGoodsList(new LinkedList<>());
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(request.getOrderId());

            double orderTotal = 0;
            for (OrderItem orderItem : orderItems) {
                OrderIdDetailResponse.GoodsListDTO goodsListDTO = new OrderIdDetailResponse.GoodsListDTO();
                BeanUtils.copyProperties(orderItem, goodsListDTO);
                goodsListDTO.setOrderStatus(orderItem.getStatus());
                response.getGoodsList().add(goodsListDTO);
                orderTotal += orderItem.getTotalFee();
            }

            response.setOrderTotal((int) orderTotal);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(request.getOrderId());
            response.setUserName(orderShipping.getReceiverName());
            response.setTel(orderShipping.getReceiverPhone());
            response.setStreetName(orderShipping.getReceiverAddress());
            response.setOrderStatus(order.getStatus());

            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("com.mall.order.services.OrderQueryServiceImpl occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
